var toolsId = getQueryString("id");
var mapNo = getQueryString("mapNo");
$(function () {
    $("#jqGrid").Grid({
        url: '../toolsfile/list?toolsId=' + toolsId,
        colModel: [
            {label: '唯一标识', name: 'unNo', index: 'UN_NO', width: 80},
            {label: '工装图号', name: 'toolsMapNo', index: 'TOOLS_MAP_NO', width: 80},
            {label: '版本号', name: 'toolsEdNo', index: 'TOOLS_ED_NO', width: 80},
            {label: '编号', name: 'toolsNo', index: 'TOOLS_NO', width: 80},
            {
                label: '档案类型', name: 'type', index: 'TYPE', width: 80,
                formatter: function (value) {
                    return sysEnums.TOOLS_FILE_TYPE[value];
                }
            },
            {label: 'ME工装/工艺工程师', name: 'deptApprovalName', index: 'DEPT_APPROVAL', width: 80},
            /*{label: '审核状态', name: 'deptApprovalStatus', index: 'DEPT_APPROVAL_STATUS', width: 80,
                formatter: function (value) {
                    return sysEnums.CHECK_FLAG[value];
                }
            },*/
            // {label: '审核时间', name: 'deptApprovalDate', index: 'DEPT_APPROVAL_DATE', width: 80,
            //     formatter: function (value) {
            //         return transDate(value,"yyyy-MM-dd");
            //     }
            // },
            /*    {label: '车间质控主任', name: 'leaderApproval', index: 'LEADER_APPROVAL', width: 80},
                {label: '审核状态', name: 'leaderApprovalStatus', index: 'LEADER_APPROVAL_STATUS', width: 80,
                    formatter: function (value) {
                        return sysEnums.CHECK_FLAG[value];
                    }
                },
                {label: '审核时间', name: 'leaderApprovalDate', index: 'LEADER_APPROVAL_DATE', width: 80,
                    formatter: function (value) {
                        return transDate(value,"yyyy-MM-dd");
                    }
                },*/
            {
                label: '知识库', name: 'knowBase', index: 'KNOW_BASE', width: 80,
                formatter: function (value) {
                    return sysEnums.REPAIR_IN_KNOW[value];
                }
            },
            {label: '创建人', name: 'createUserName', index: 'CREATE_USER', width: 80,},
            {label: '工装ID', name: 'toolsId', index: 'TOOLS_ID', width: 80, hidden: true},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: '知识库', name: 'know', index: 'KNOW_BASE', width: 80, hidden: true,
                formatter: function (value, col, row) {
                    return row.knowBase;
                }
            },
            {label: '知识类型', name: 'konwTypeName', index: 'KONW_TYPE_NAME', width: 80},
            {
                label: 'deptStatus', name: 'deptStatus', index: 'DEPT_APPROVAL_STATUS', width: 80, hidden: true,
                formatter: function (value, col, row) {
                    return row.deptApprovalStatus;
                }
            },
            {
                label: 'leaderStatus', name: 'leaderStatus', index: 'LEADER_APPROVAL_STATUS', width: 80, hidden: true,
                formatter: function (value, col, row) {
                    return row.leaderApprovalStatus;
                }
            },
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsFile: {
            delFlag: '',
            createDate: '',
            createUser: '',
            knowBase: '',
            toolsNo: '',
            toolsEdNo: '',
            toolsMapNo: '',
            id: '',
            type: '',
            leaderApprovalDate: '',
            leaderApprovalStatus: '',
            leaderApproval: '',
            deptApprovalDate: '',
            deptApprovalStatus: '',
            deptApproval: '',
            updateDate: '',
            updateUser: '',
            toolsId: '',
            unNo: '',
            checkResult: '',
            konwTypeName: ''
        },
        ruleValidate: {
            type: [
                {type: 'number', required: true, message: '档案类型不能为空', trigger: 'change'}
            ]
        },
        q: {
            toolsMapNo:''
        },
        fileTypeArray: enumsToList('TOOLS_FILE_TYPE'),
    },
    methods: {
        deptCheck: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.deptStatus !== '0') {
                alert("已审核！");
                return;
            }

            openWindow({
                title: "确认审核",
                area: ['350px', '200px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    vm.toolsFile = {deptApprovalStatus: vm.toolsFile.checkResult, id: data.id};
                    var url = '../toolsfile/deptCheck';
                    var params = JSON.stringify(vm.toolsFile);
                    vm.updateCheck(url, params)
                }
            });
        },
        /*    leaderCheck: function(){
                var data = getSelectedRowData("#jqGrid");

                if(data.id == null){
                    return;
                }

                if(data.deptStatus == '2'){
                    alert("ME工装/工艺工程师审核不通过");
                    return;
                }

                if(data.leaderStatus !== '0'){
                    alert("已审核！");
                    return;
                }

                openWindow({
                    title: "确认审核",
                    area: ['350px', '200px'],
                    content: jQuery("#reviewLayer"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        vm.toolsFile = {leaderApprovalStatus: vm.toolsFile.checkResult,id: data.id };
                        var url = '../toolsfile/leaderCheck';
                        var params = JSON.stringify(vm.toolsFile);
                        vm.updateCheck(url, params)
                    }
                });
            },*/
        updateCheck: function (url, params) {
            Ajax.request({
                url: url,
                params: params,
                contentType: "application/json",
                type: 'POST',
                successCallback: function (res) {
                    alert('审核完成!');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                }
            });
        },

        addKnowBase: function () {
            var data = getSelectedRowData("#jqGrid");
            if (data.id == null) {
                return;
            }
            if (data.know == '1') {
                alert("已纳入知识库");
                return;
            }
            if (data.konwTypeName == null || data.konwTypeName == '') {
                alert("知识类型未赋值");
                return;
            }


            vm.toolsFile = {id: data.id,};


            Ajax.request({
                url: '../toolsfile/addKnowBase',
                params: JSON.stringify(vm.toolsFile),
                contentType: "application/json",
                type: 'POST',
                successCallback: function (res) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        goToolsEqu: function () {
            var data = getSelectedRowData("#jqGrid");
            if (data.id == null) {
                return;
            }
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 附件信息',
                content: '../sys/equFile.html?type=3&id=' + data.id + '&createUserName=' + data.createUserName
            })
        },
        query: function () {
            vm.reload();
        },

        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsFile = {
                delFlag: '',
                createDate: '',
                createUser: '',
                knowBase: '',
                toolsNo: '',
                toolsEdNo: '',
                toolsMapNo: mapNo,
                type: '',
                leaderApprovalDate: '',
                leaderApprovalStatus: '',
                leaderApproval: '',
                deptApprovalDate: '',
                deptApprovalStatus: '',
                deptApproval: '',
                updateDate: '',
                updateUser: '',
                toolsId: toolsId,
                unNo: '',
                konwTypeName: ''
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.toolsFile.id == null ? "../toolsfile/save" : "../toolsfile/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsFile),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolsfile/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolsfile/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsFile = r.toolsFile;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'toolsMapNo':vm.q.toolsMapNo},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                toolsMapNo:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getSpare: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-bars"></i> 选择知识类型',
                content: '../sys/sysknowtype.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSelectedRowData("#jqGrid");
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.toolsFile.konwTypeName = res.konwName;

                    }
                }
            })
        }
    }
});
